package com.itheima.control;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.domain.Book;
import com.itheima.utils.Result;
import com.itheima.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author tongchen
 * @create 2022-12-17 18:30
 */
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    IBookService bookService;
    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id){
        return new Result(true,bookService.getById(id));

    }
    @GetMapping
    public Result getAll(){
        int i=1/0;
        return new Result(true,bookService.list());
    }
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id){
      return  new Result(bookService.removeById(id));
    }
    @PostMapping
    public Result save(@RequestBody Book book){
      return new Result(bookService.save(book));
    }
    @PutMapping
    public Result update(@RequestBody Book book){
     return  new Result(bookService.save(book));
    }
    @GetMapping("{column}/{num}")
    public Result selectByPage(@PathVariable Integer column,@PathVariable Integer num,Book book){
        IPage<Book> iPage = bookService.selectByPage(column, num, book);
        if(column>iPage.getPages()){
            iPage=bookService.selectByPage((int) iPage.getPages(),num,book);

        }
        return new Result(true,iPage);
    }

}
